package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.IH_SMainActivityContract;
import com.clouddot.baipai.http.entity.response.addEvaluateResponse;
import com.clouddot.baipai.http.entity.response.getBanPaiModelResponse;
import com.clouddot.baipai.http.entity.response.getClassInfoResponse;
import com.clouddot.baipai.http.entity.response.getCommitteeResponse;
import com.clouddot.baipai.http.entity.response.getIndexListResponse;
import com.clouddot.baipai.http.entity.response.getPersonInfoResponse;
import com.clouddot.baipai.http.entity.response.loginResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.util.viewUtil.getTimeAndWeatherUtil;

import org.dom4j.DocumentException;
import org.wxy.sdk.base.impl.BasePresenterImpl;

import java.util.ArrayList;

import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class H_SMainActivityPresenter extends BasePresenterImpl<IH_SMainActivityContract.IView, IH_SMainActivityContract.IModel> implements IH_SMainActivityContract.IPresenter {
    public H_SMainActivityPresenter(IH_SMainActivityContract.IView view, IH_SMainActivityContract.IModel model) {
        super(view, model);
    }

    @Override
    public void getClassInfo(String carddevid) {
        Subscription subscription = mModel.getClassInfo("GetClassInfo", carddevid)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getClassInfoResponse>() {
                               @Override
                               public void call(getClassInfoResponse response) {
                                   if (response.ClassId != null) {
                                       Env.getAppContext().ClassID = response.ClassId;
                                       mView.success(1, response);
                                   } else {
                                       mView.fail(1, "班级信息获取失败");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(1, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void login(final String code) {
        Subscription subscription = mModel.login("CardLogin", code)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<loginResponse>() {
                               @Override
                               public void call(loginResponse response) {
                                   if (!response.res.equals("fail")) {
                                       Env.getAppContext().cardID = code;
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "登录失败");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void login(final String code, String filepath) {
        Subscription subscription = mModel.loginByPhoto("CardLogin", code, filepath)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<loginResponse>() {
                               @Override
                               public void call(loginResponse response) {
                                   if (!response.res.equals("fail")) {
                                       Env.getAppContext().cardID = code;
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "登录失败");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getPerson() {
        Subscription subscription = mModel.getPerson("GetLoginInfo")
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getPersonInfoResponse>() {
                               @Override
                               public void call(getPersonInfoResponse response) {
                                   if (response.StudentId != null && response.ClassId.equals(Env.getAppContext().ClassID)) {
                                       Env.getAppContext().StudentID = response.StudentId;
                                       mView.success(3, response);
                                   } else {
                                       mView.fail(3, "你不属于这个班级");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(3, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getWeather(String cityKey) {
        Subscription subscription = mModel.getWeather(cityKey)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<String>() {
                               @Override
                               public void call(String result) {
                                   try {
                                       mView.success(4, getTimeAndWeatherUtil.getWeatherInfo(result));
                                   } catch (DocumentException e) {
                                       e.printStackTrace();
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(4, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getBanPaiModel() {
        Subscription subscription = mModel.getBanPaiModel("GetSysParamValue", "ClassCardParam")
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getBanPaiModelResponse>() {
                               @Override
                               public void call(getBanPaiModelResponse response) {
                                   mView.success(5, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(5, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getCommittee(String ClassId, String code) {
        Subscription subscription = mModel.getCommittee("QueryClassValUser", ClassId,code)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<ArrayList<getCommitteeResponse>>() {
                               @Override
                               public void call(ArrayList<getCommitteeResponse> response) {
                                   if(!response.isEmpty()) {
                                       Env.getAppContext().UserId = response.get(0).UserId;
                                       Env.getAppContext().ValType = response.get(0).ValType;
                                       Env.getAppContext().WeekId = response.get(0).WeekId;
                                       mView.success(6, response);
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(6, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }
    @Override
    public void addEvaluate(String ClassId, String ObjectId, String ValType, String UserId, String WeekId,String value) {
        Subscription subscription = mModel.addEvaluate("ClassValAdd", ClassId,ObjectId,ValType,UserId,WeekId,value)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<addEvaluateResponse>() {
                               @Override
                               public void call(addEvaluateResponse response) {
                                   mView.success(7, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(7, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }

    @Override
    public void getIndexList(String ObjectType, int pageindex, int pagesize) {
        Subscription subscription = mModel.getIndexList("QueryForClassValObject", ObjectType,pageindex,pagesize)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<getIndexListResponse>() {
                               @Override
                               public void call(getIndexListResponse response) {
                                   mView.success(8, response);
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(8, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }


}
